package com.njupt.sellsec.validator;

import com.njupt.sellsec.util.ValidatorUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 15:10 2018/7/28
 * @Modified By:
 **/
public class IsMobileValidator implements ConstraintValidator<IsMobile,String>{

    private boolean required = false;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (required){
            return ValidatorUtil.isMobile(s);

        }else {
            if (StringUtils.isEmpty(s)){
                return true;
            }else {
                return ValidatorUtil.isMobile(s);
            }

        }

    }
}
